<script setup>
import {reqAddMenu, reqAllMenuTree, reqMenuByMenuType, reqRemoveBatchMenu, reqUpdateMenu} from "@/api/modules/menu.js";
import {ref, watch} from "vue";
import {ElMessage} from "element-plus";

const tableData = ref()
const parentMenu = ref()
const defaultExpandAll = ref(true)
const treeKey = ref('')
const switchExpandAll = () => {
  treeKey.value = new Date()
  defaultExpandAll.value = !defaultExpandAll.value
}
const init = async () => {
  tableData.value = await reqAllMenuTree()
  parentMenu.value = await reqMenuByMenuType("M")
}

const showAddOrUpdate = ref()
const defaultAddModel = {
  menuId: '',
  pid: '',
  label: '',
  menuType: 'M',
  orderBy: 0,
  code: '',
  path: '',
  name: '',
  redirect: '',
  componentPath: '',
  isHidden: 0,
  icon: '',
  remark: ''
}
const addOrUpdateModel = ref(JSON.parse(JSON.stringify(defaultAddModel)))
const handleAdd = (row) => {
  if (row) {
    addOrUpdateModel.value.pid = row.menuId
  }
  showAddOrUpdate.value = true
}

const closeAdd = () => {
  showAddOrUpdate.value = false
  addOrUpdateModel.value = JSON.parse(JSON.stringify(defaultAddModel))
}

const confirmAdd = async () => {
  // 存在id，则为修改
  if (addOrUpdateModel.value.menuId) {
    await reqUpdateMenu(addOrUpdateModel.value)
  } else {
    await reqAddMenu(addOrUpdateModel.value)
  }
  closeAdd()
  ElMessage.success("保存成功")
  await init()
}

const handleEdit = (row) => {
  showAddOrUpdate.value = true
  addOrUpdateModel.value = JSON.parse(JSON.stringify(row))
}

const showDetails = ref(false)
const detailsModel = ref({})
const details = (row) => {
  showDetails.value = true
  detailsModel.value = row
}

const closeDetails = () => {
  showDetails.value = false
  detailsModel.value = {}
}

const remove = async (row) => {
  const ids = [row.menuId]
  getChildrenIds(row.children, ids)
  await reqRemoveBatchMenu(ids)
  ElMessage.success("删除成功")
  await init()
}

const getChildrenIds = (children, ids) => {
  if (!children){
    return
  }
  children.forEach(item => {
    ids.push(item.menuId)
    if (item.children && item.children.length > 0) {
      getChildrenIds(item.children, ids)
    }
  })
}
watch(() => addOrUpdateModel.value.menuType, (newValue, oldValue) => {
  if (newValue === 'B' && oldValue === 'M') {
    addOrUpdateModel.value.path = ''
    addOrUpdateModel.value.name = ''
    addOrUpdateModel.value.redirect = ''
    addOrUpdateModel.value.componentPath = ''
  }
})

init()
</script>

<template>

  <!--  查看菜单详情-->
  <el-drawer v-model="showDetails" title="菜单详情" :with-header="false" @close="closeDetails">
    <el-form label-width="100" :model="detailsModel" disabled>
      <el-form-item label="菜单ID">
        <el-input v-model="detailsModel.menuId"></el-input>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="detailsModel.label" placeholder="请输入新菜单名称"></el-input>
      </el-form-item>
      <el-form-item label="菜单类型">
        <el-radio-group v-model="detailsModel.menuType" class="ml-4">
          <el-radio value="M" size="large">目录</el-radio>
          <el-radio value="B" size="large">按钮</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序号">
        <el-input-number v-model="detailsModel.orderBy"></el-input-number>
      </el-form-item>
      <el-form-item label="菜单代码">
        <el-input v-model="detailsModel.code"></el-input>
      </el-form-item>
      <div v-if="detailsModel.menuType==='M'">
        <el-form-item label="访问路径">
          <el-input v-model="detailsModel.path"></el-input>
        </el-form-item>
        <el-form-item label="路由名称">
          <el-input v-model="detailsModel.name"></el-input>
        </el-form-item>
        <el-form-item label="重定向">
          <el-input v-model="detailsModel.redirect"></el-input>
        </el-form-item>
        <el-form-item label="组件位置">
          <template #label>
            <div title="vue组件的位置">
              <span>组件位置</span> <span style="margin-left: 5px"
                                          class="iconfont icon-wenhao"></span>
            </div>
          </template>
          <el-input v-model="detailsModel.componentPath"></el-input>
        </el-form-item>
      </div>
      <el-form-item label="是否隐藏">
        <template #label>
          <div title="是否在侧边栏显示">
            <span>是否隐藏</span> <span style="margin-left: 5px"
                                        class="iconfont icon-wenhao"></span>
          </div>
        </template>
        <el-radio-group v-model="detailsModel.isHidden" class="ml-4">
          <el-radio :value="0" size="large">显示</el-radio>
          <el-radio :value="1" size="large">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="icon图标">
        <el-input v-model="detailsModel.icon"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="detailsModel.remark"></el-input>
      </el-form-item>
    </el-form>
  </el-drawer>


  <!--  新增/修改 菜单弹出框-->
  <el-dialog v-model="showAddOrUpdate" title="Tips" width="500" draggable @close="closeAdd">
    <template #header>
      <h3>{{addOrUpdateModel.menuId?'编辑菜单':'新增菜单'}}</h3>
    </template>
    <el-form class="form" label-width="100" :model="addOrUpdateModel">
      <el-form-item label="父级菜单">
        <el-select v-model="addOrUpdateModel.pid" placeholder="选择父级菜单" clearable>
          <el-option
              v-for="item in parentMenu"
              :key="item.menuId"
              :label="item.label"
              :value="item.menuId"
          />
        </el-select>
        <!--        </div>-->
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="addOrUpdateModel.label" placeholder="请输入新菜单名称"></el-input>
      </el-form-item>
      <el-form-item label="菜单类型">
        <el-radio-group v-model="addOrUpdateModel.menuType" class="ml-4">
          <el-radio value="M" size="large">目录</el-radio>
          <el-radio value="B" size="large">按钮</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序号">
        <el-input-number v-model="addOrUpdateModel.orderBy"></el-input-number>
      </el-form-item>
      <el-form-item label="菜单代码">
        <el-input v-model="addOrUpdateModel.code"></el-input>
      </el-form-item>
      <div v-if="addOrUpdateModel.menuType==='M'">
        <el-form-item label="访问路径">
          <el-input v-model="addOrUpdateModel.path"></el-input>
        </el-form-item>
        <el-form-item label="路由名称">
          <el-input v-model="addOrUpdateModel.name"></el-input>
        </el-form-item>
        <el-form-item label="重定向">
          <el-input v-model="addOrUpdateModel.redirect"></el-input>
        </el-form-item>
        <el-form-item label="组件位置">
          <template #label>
            <div title="vue组件的位置">
              <span>组件位置</span> <span style="margin-left: 5px"
                                          class="iconfont icon-wenhao"></span>
            </div>
          </template>
          <el-input v-model="addOrUpdateModel.componentPath"></el-input>
        </el-form-item>
      </div>
      <el-form-item label="是否隐藏">
        <template #label>
          <div title="是否在侧边栏显示">
            <span>是否隐藏</span> <span style="margin-left: 5px"
                                        class="iconfont icon-wenhao"></span>
          </div>
        </template>
        <el-radio-group v-model="addOrUpdateModel.isHidden" class="ml-4">
          <el-radio :value="0" size="large">显示</el-radio>
          <el-radio :value="1" size="large">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="icon图标">
        <el-input v-model="addOrUpdateModel.icon"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="addOrUpdateModel.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="confirmAdd" style="width: 60%">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>

  <!--  表格展示-->
  <el-card>
    <template #header>
      <el-button @click="handleAdd" type="primary" v-hasPerm="'perm:menu:dml'">新增菜单</el-button>
      <el-button @click="switchExpandAll">{{ !defaultExpandAll ? '展开全部' : '折叠全部' }}</el-button>
    </template>
    <el-table
        :data="tableData"
        row-key="menuId"
        border
        :key="treeKey"
        :default-expand-all="defaultExpandAll"
        show-overflow-tooltip
        :tree-props="{ children: 'children'  }"
    >
      <el-table-column label="名称">
        <template #default="{row}">
          <el-link :underline="false" @click="details(row)">
            <span>{{ row.label }}</span> <span class="iconfont icon-eye" style="margin-left: 5px"></span>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="权限代码"/>
      <el-table-column prop="createTime" label="创建时间"/>
      <el-table-column label="操作" v-hasPerm="'perm:menu:dml'">
        <template #default="{row}">
          <el-button v-if="row.menuType==='M'" circle class="iconfont icon-add" @click="handleAdd(row)"></el-button>
          <el-button v-if="row.menuId!==0" circle class="iconfont icon-xiugai" @click="handleEdit(row)"></el-button>
          <el-popconfirm title="Are you sure to delete this?" @confirm="remove(row)">
            <template #reference>
              <el-button v-if="row.menuId!==0" circle class="iconfont icon-shanchu"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<style lang="scss" scoped>
.el-card {
  padding: 0;
  height: 100%;

  :deep .el-card__header {
    height: 50px;
    padding: 10px;
  }

  :deep .el-card__body {
    height: calc(100% - 50px);
    padding: 10px;
  }

  .el-table {
    height: calc(100%);
  }
}

.form {
  padding: 0 10px;
  overflow: auto;
  height: 400px;

  .el-form-item {
    margin: 5px 0;
  }
}
</style>
